import {
  GetSealInfo,
  PostDeleteData,
  PostSealInfo
} from "/constData/requestFun/_httpRequest";
import {
  GetUseSeal,
  GetUseSealById,
  PostUseSeal,
  UseSealDeleteData
} from "/constData/requestFun/_teseRequest";
import compact from 'loadsh/compact'
import {
  LOG,
  POLICIES
} from '/constData/_';
Page({
  data: {
    approve: [],
    options: [{
        value: '公章',
        label: '公章'
      },
      {
        value: '财务章',
        label: '财务章'
      },
      {
        value: '法定代表人章',
        label: '法定代表人章'
      },
      {
        value: '发票专用章',
        label: '发票专用章'
      },
      {
        value: '其他章',
        label: '其他章'
      },
      {
        value: '公司章程',
        label: '公司章程'
      },
      {
        value: '营业执照',
        label: '营业执照'
      }
    ],
    list: ['使用印章', '用印外借'],
    item: 1,
    min: null,
    max: new Date('2033/12/02'),
    defaultDate: new Date(),
    sealinfo: {
      CorpId: '', //申请人 
      StampType: '使用印章', //用印类型
      ApplicatDepart: '', //申请部门
      HandledBy: '', //经办人
      Date: new Date(), //日期
      SealFile: '', //用印文件
      ReasonBorrow: '', //借用事由
      StartDate: new Date(), //开始时间
      EndDate: new Date(), //结束时间
      SealType: '公章', //印章类型
      Note:'',
      OtherSeal: '', //其他章
    },
    ReceiveData: []
  },
  handleOk(value, column, e) {
    this.setData({
      sealinfo: {
        ...this.data.sealinfo,
        StampType: value
      },
    })
  },
  Delete(e) {
    const id = e.currentTarget.dataset.id
    UseSealDeleteData(id, (res) => {
      my.alert({
        title: '撤销成功',
      });
      //刷新数据
      this.setData({
        ReceiveData: this.data.ReceiveData.filter((it) => {
          return it.item1.id !== id
        })
      })
    }, (err) => {
      my.alert({
        title: '撤销失败',
      });
    })
  },
  handleOk1(date, format, e) {
    this.setData({
      sealinfo: {
        ...this.data.sealinfo,
        Date: date,
        EndDate: date
      },
    })
  },
  handleOk2(date, format, e) {
    this.setData({
      sealinfo: {
        ...this.data.sealinfo,
        StartDate: date,
        EndDate: date
      },
      min: date
    })
  },
  handleOk3(date, format, e) {
    this.setData({
      sealinfo: {
        ...this.data.sealinfo,
        EndDate: date
      },
    })
  },
  onApplicatDepartChange(e) {
    this.setData({
      sealinfo: {
        ...this.data.sealinfo,
        ApplicatDepart: e
      }
    })
  },
  onHandledByChange(e) {
    this.setData({
      sealinfo: {
        ...this.data.sealinfo,
        HandledBy: e
      }
    })
  },
  onReasonBorrowChange(e) {
    this.setData({
      sealinfo: {
        ...this.data.sealinfo,
        ReasonBorrow: e
      },
      approve: POLICIES.SealApplicantion_POLICIES()
    })
  },
  onTotalamountChange(e) {
    this.setData({
      sealinfo: {
        ...this.data.sealinfo,
        Totalamount: e
      }
    })
  },
  onnotesChange(e) {
    this.setData({
      sealinfo: {
        ...this.data.sealinfo,
        notes: e
      }
    })
  },
  onprocurementmethodChange(e) {
    this.setData({
      sealinfo: {
        ...this.data.procurementmethod,
        procurementmethod: e
      }
    })
  },
  handleChange(value) {
    this.setData({
      sealinfo: {
        ...this.data.sealinfo,
        SealType: value.join(',')
      }
    });
    if (this.data.sealinfo.SealType.includes('其他章')) {
      this.setData({
        sealinfo: {
          ...this.data.sealinfo,
          Note: 'true'
        }
      });
    }
    else{
      this.setData({
        sealinfo: {
          ...this.data.sealinfo,
          Note: 'false'
        }
      });
    }
  },
  onOtherSealChange(e) {
    this.setData({
      sealinfo: {
        ...this.data.sealinfo,
        OtherSeal: e
      }
    })
  },
  // 上传数据
  onSubmit(e) {
    //待以后优化
    // this.setData({
    //   sealinfo: {
    //     ...this.data.sealinfo,
    //     SealType: this.data.sealinfo.SealType.join(',')
    //   }
    // });
    //console.log('提交数据',JSON.stringify(this.data.sealinfo));
    PostUseSeal(JSON.stringify(this.data.sealinfo),
      (res) => {
        my.alert({
          title: '提交成功',
        });
        GetUseSealById(res.data, (res) => {
          this.setData({
            ReceiveData: compact([...this.data.ReceiveData, res.data].map(it => {
              if (it.item1.corpId === this.data.sealinfo.CorpId) {
                return it
              }
            })).reverse()
          })
        })
        this.setData({
          sealinfo: {
            ...this.data.sealinfo,
            ApplicatDepart: '', //申请部门
            HandledBy: '', //经办人
            Date: new Date(), //日期
            SealFile: '', //用印文件
            ReasonBorrow: '', //借用事由
            StartDate: new Date(), //开始时间
            EndDate: new Date(), //结束时间
            SealType: '公章', //印章类型
          }
        })
      },
      (err) => {
        my.alert({
          title: '提交成功1',
        });
      })
  },

  //获取数据
  onLoad() {
    my.getStorage({
      key: 'userInfo',
      success: (storage) => {
        GetUseSeal((res) => {
          const cacheData = res.data.map(it => {
            if (it.item1.corpId === storage.data.result.userid) {
              return it
            }
          })
          this.setData({
            sealinfo: {
              ...this.data.sealinfo,
              CorpId: storage.data.result.userid
            },
            ReceiveData: compact(cacheData).reverse()
          })
        })
      }
    })
  },
  bindButtonTap() {
    this.onFocus();
  },
  bindTextAreaBlur(e) {
    console.log(e.detail.value);
  },
});